/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNutri.java
 *
 * Created on Aug 14, 2011, 11:07:29 AM
 */
package GUI;

import Admin.CopyFile;
import Class.Nutrition;
import Admin.InitData;
import Class.ImageFilter;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Tomcat
 */
public class NutriAddNew extends javax.swing.JDialog {

    private String des;
    private String src;

    /** Creates new form AddNutri */
    public NutriAddNew(JFrame parent,boolean modal) {
        super(parent,modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNutriName = new javax.swing.JTextField();
        btnBrowse = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtaNutriContent = new javax.swing.JTextArea();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblImgName = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lblViewImg = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        rbActive = new javax.swing.JRadioButton();
        rbInactive = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ADD NEW NUTRITION");
        setBackground(new java.awt.Color(204, 255, 204));
        setModal(true);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Century Gothic", 1, 14)); // NOI18N
        jLabel1.setText("ADD NUTRITION ");
        jPanel1.add(jLabel1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jLabel2.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        jLabel2.setText("Nutrition Name");

        jLabel3.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        jLabel3.setText("Nutrition Image");

        jLabel4.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        jLabel4.setText("Nutrition Content");

        btnBrowse.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        btnBrowse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Item_news.png"))); // NOI18N
        btnBrowse.setText("Browse");
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });

        txtaNutriContent.setColumns(20);
        txtaNutriContent.setLineWrap(true);
        txtaNutriContent.setRows(5);
        txtaNutriContent.setWrapStyleWord(true);
        jScrollPane1.setViewportView(txtaNutriContent);

        btnOK.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        btnOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/oke.png"))); // NOI18N
        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/cancel.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(lblViewImg);

        jLabel5.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        jLabel5.setText("Status");

        buttonGroup1.add(rbActive);
        rbActive.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        rbActive.setSelected(true);
        rbActive.setText("Active");

        buttonGroup1.add(rbInactive);
        rbInactive.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        rbInactive.setText("Inactive");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnOK)
                        .addGap(26, 26, 26)
                        .addComponent(btnCancel))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnBrowse)
                        .addGap(77, 77, 77)
                        .addComponent(lblImgName, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtNutriName, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(rbActive)
                        .addGap(28, 28, 28)
                        .addComponent(rbInactive)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNutriName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(rbActive)
                    .addComponent(rbInactive))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(btnBrowse, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblImgName, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnOK)
                            .addComponent(btnCancel))
                        .addContainerGap(22, Short.MAX_VALUE))))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean CheckValid() {
        String nutriName = txtNutriName.getText();
        String content = txtaNutriContent.getText();
        if (nutriName.equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(this, "Please input Name", "ERROR", JOptionPane.OK_OPTION);
            return false;
        }
        if (content.equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(this, "Please input Content", "ERROR", JOptionPane.OK_OPTION);
            return false;
        }
        return true;
    }
    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        String nutriName = txtNutriName.getText();
        String nutriImage = "";
        String nutriContent = txtaNutriContent.getText();
        String status = "1";
        if (src.equals("") || des.equals("")) { //if user doesn't choose any images
            nutriImage = "";
        } else {
            File newFile = CopyFile.FileCopy(src, des);
            nutriImage = newFile.toString();
        }
        if (rbInactive.isSelected()) {
            status = "0";
        }
        if (CheckValid()) {
            Nutrition nutri = new Nutrition(nutriName, nutriContent, nutriImage, status);
            int resultAdd = InitData.AddNutri(nutri);
            if (resultAdd > 0) {
                JOptionPane.showMessageDialog(this, "Add New Nutrition Successfully!");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Add New Nutrition Fail!");
            }
        }
    }//GEN-LAST:event_btnOKActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        JFileChooser imgChooser = new JFileChooser();
        String url = "";
        imgChooser.setCurrentDirectory(new File("src\\Images"));
        imgChooser.setFileFilter(new ImageFilter());
        int chooser = imgChooser.showOpenDialog(this);
        if (chooser == JFileChooser.APPROVE_OPTION) {
            File f = imgChooser.getSelectedFile();
            url = f.toString();
            String file = null;
            file = url.substring(url.lastIndexOf("\\"));
            this.src = url;
            this.des = "src\\Images" + file;
            lblImgName.setText(f.getName());
        }
        lblViewImg.setIcon(new ImageIcon(url));
    }//GEN-LAST:event_btnBrowseActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                AddNutri dialog = new AddNutri(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblImgName;
    private javax.swing.JLabel lblViewImg;
    private javax.swing.JRadioButton rbActive;
    private javax.swing.JRadioButton rbInactive;
    private javax.swing.JTextField txtNutriName;
    private javax.swing.JTextArea txtaNutriContent;
    // End of variables declaration//GEN-END:variables
}
